package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiT_shipper_account;
import cn.com.api.service.ApiT_shipper_accountService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "shipper/")
public class ApiT_shipper_accountController {
	@Autowired
	private ApiT_shipper_accountService apiT_shipper_accountService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger
			.getLogger(ApiT_shipper_accountController.class);

	/**
	 * 配送端  我的账户信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "getMyCount")
	public void getMyCount(HttpServletRequest request, HttpServletResponse response) throws Exception {
		/**
		 * 配送费收入 、配送奖励、其他奖励、纠错、货到付款、已提现金额、账户余额
		 */
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			String u_id = request.getParameter("u_id");
			String token = request.getParameter("token");
			Map<String,String> params = new HashMap<String,String>();
			params.put("u_id", u_id);
			if(checkService.checkToken(u_id, token, map)){
				apiT_shipper_accountService.getMyCount(params, map);
			}
			
		}catch(Exception e){
			logger.error(Util.getStackTraceAsString(e));
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
		
	}
	
	/**
	 * 配送端的状态   在岗、离岗、小休
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "getShipperStatus")
	public void getShipperStatus(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		try{
			String u_id = request.getParameter("u_id");
			String token = request.getParameter("token");
			Map<String,String> params = new HashMap<String,String>();
			params.put("u_id", u_id);
			if(checkService.checkToken(u_id, token, map)){
				apiT_shipper_accountService.getShipperStatus(params, map);
			}
		}catch(Exception e){
			logger.error(Util.getStackTraceAsString(e));
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 * 修改配送员的在岗状态
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "modifyShipperStatus")
	public void modifyShipperStatus(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		
		try{
			String u_id = request.getParameter("u_id");
			String token = request.getParameter("token");
			String status = request.getParameter("working_status");//0： 离岗 1：在岗 2：小休
			Map<String,String> params = new HashMap<String,String>();
			params.put("u_id", u_id);
			params.put("status", status);
			if(checkService.checkToken(u_id, token, map)){
				apiT_shipper_accountService.modifyShipperStatus(params, map);
			}
		}catch(Exception e){
			logger.error(Util.getStackTraceAsString(e));
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 * 新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "insertT_shipper_account")
	public void insertT_shipper_account(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String user_id = request.getParameter("user_id");
		String working_status = request.getParameter("working_status");
		String money = request.getParameter("money");
		String order_cash = request.getParameter("order_cash");
		String create_time = request.getParameter("create_time");
		String update_time = request.getParameter("update_time");

		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (user_id != null && !"".equals(user_id.trim())) {
				ApiT_shipper_account t_shipper_account = new ApiT_shipper_account();

				t_shipper_account.setUser_id(user_id);
				t_shipper_account.setWorking_status("0");
				// t_shipper_account.setCreate_time(new Date());

				apiT_shipper_accountService
						.insertT_shipper_account(t_shipper_account);
				// return
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			}
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_shipper_accountById")
	public void findT_shipper_accountById(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			ApiT_shipper_account t_shipper_account = apiT_shipper_accountService
					.findT_shipper_accountById(Integer.valueOf(id));

			// return
			map.put("datas", t_shipper_account);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();

	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findShipperInfoByC_id")
	public void findShipperInfoByC_id(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String c_id = request.getParameter("c_id");
			Map<String, Object> t_shipper = apiT_shipper_accountService
					.findShipperInfoByC_id(c_id);

			// return
			map.put("datas", t_shipper);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();

	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateT_shipper_account")
	public void updateT_shipper_account(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		String working_status = request.getParameter("working_status");
		String money = request.getParameter("money");
		String order_cash = request.getParameter("order_cash");
		String create_time = request.getParameter("create_time");
		String update_time = request.getParameter("update_time");

		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {
				ApiT_shipper_account t_shipper_account = new ApiT_shipper_account();
				// 获取原数据
				t_shipper_account = apiT_shipper_accountService
						.findT_shipper_accountById(Integer.valueOf(id));
				// 修改
				t_shipper_account.setWorking_status(working_status);
				t_shipper_account.setMoney(Util.getBigDecimal(money));
				//
				apiT_shipper_accountService
						.updateT_shipper_account(t_shipper_account);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 修改配送员工作状态
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateT_shipper_accountStatus")
	public void updateT_shipper_accountStatus(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String user_id = request.getParameter("user_id");
		String working_status = request.getParameter("working_status");

		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (user_id != null && !"".equals(user_id.trim())) {
				ApiT_shipper_account t_shipper_account = new ApiT_shipper_account();
				// 获取原数据
				List<ApiT_shipper_account> t_shipper_accounts = apiT_shipper_accountService
						.findT_shipper_accountsByUser_id(user_id);
				t_shipper_account = t_shipper_accounts.get(0);
				// 修改工作状态
				t_shipper_account.setWorking_status(working_status);
				//
				apiT_shipper_accountService
						.updateT_shipper_account(t_shipper_account);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteT_shipper_account")
	public void deleteT_shipper_account(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {

				Integer ret_I = apiT_shipper_accountService
						.deleteT_shipper_account(Integer.parseInt(id));
				if (ret_I > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "删除成功！");
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "检查失败！");
				}
			}
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
